// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#ifndef MGCBEZIERRECTANGLE3_H
#define MGCBEZIERRECTANGLE3_H

#include "MgcBezierRectangle.h"
#include "MgcColor.h"
#include "MgcVector2.h"
#include "MgcVector3.h"


class MgcBezierRectangle3 : public MgcBezierRectangle
{
    MgcDeclareRTTI;
    MgcDeclareStream;

public:
    // Construction.  MgcBezierRectangle3 accepts responsibility for deleting
    // the input array.
    MgcBezierRectangle3 (unsigned int* auiIndex);

    // tessellation
    virtual void Tessellate (unsigned int uiLevel,
        const MgcVector3* akCtrlPoint, const MgcColor* akCtrlColor,
        const MgcVector2* akCtrlTexture, MgcTriMesh* pkMesh,
        unsigned int& ruiVertexStart, unsigned int& ruiTriangleStart);

protected:
    MgcBezierRectangle3 ();

    // precomputation
    class BlockParameters
    {
    public:
        unsigned int m_uiI00, m_uiI01, m_uiI10, m_uiI11;

        // Xsss(0,0), Xsss(0,1), Xsss(1,0) = Xsss(0,0), Xsss(1,1) = Xsss(0,1)
        MgcVector3 m_akXsss[2];

        // Xttt(0,0), Xttt(1,0), Xttt(0,1) = Xttt(0,0), Xttt(1,1) = Xttt(1,0)
        MgcVector3 m_akXttt[2];

        // Xssstt(0,0), Xssstt(0,1), Xssstt(1,0) = Xssstt(0,0),
        //   Xssstt(1,1) = Xssstt(0,1)
        MgcVector3 m_akXssstt[2];

        // Xssttt(0,0), Xssttt(1,0), Xssttt(0,1) = Xssttt(0,0),
        //   Xssttt(1,1) = Xssttt(1,0)
        MgcVector3 m_akXssttt[2];

        MgcVector3 m_aakXss[2][2], m_aakXtt[2][2], m_aakXsstt[2][2],
            m_aakXsst[2][2], m_aakXstt[2][2];
        MgcColor m_aakCss[2][2], m_aakCtt[2][2], m_aakCsstt[2][2];
        MgcVector2 m_aakTss[2][2], m_aakTtt[2][2], m_aakTsstt[2][2];
    };

    void InitializePoints (const MgcVector3* akCtrlPoint,
        BlockParameters& rkBP);

    void InitializeNormals (const MgcVector3* akCtrlPoint,
        BlockParameters& rkBP);

    void InitializeColors (const MgcColor* akCtrlColor,
        BlockParameters& rkBP);

    void InitializeTextures (const MgcVector2* akCtrlTexture,
        BlockParameters& rkBP);

    // tessellation by recursive subdivision
    void Subdivide (unsigned int uiLevel, MgcReal fDSqr, MgcVector3* akX,
        MgcVector3* akXs, MgcVector3* akXt, MgcColor* akColor,
        MgcVector2* akTexture, BlockParameters& rkBP);
};

MgcSmartPointer(MgcBezierRectangle3);
MgcRegisterStream(MgcBezierRectangle3);

#endif
